﻿<Window x:Class="WhiteRepositoryExplorer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:helpers="clr-namespace:WhiteRepositoryExplorer.Helpers"
        xmlns:converters="clr-namespace:WhiteRepositoryExplorer.Converters"
        mc:Ignorable="d"
        Height="555"
        Width="713"
        Title="White Repository Explorer"
        DataContext="{Binding Main, Source={StaticResource Locator}}"
        Icon="Resources/Images/xml.ico"
        WindowState="Maximized">
    
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <converters:ImageNameToPathConverter x:Key="ImageNameToPathConverter" />
            <converters:RecentFilesToListOfStringsConverter x:Key="RecentFilesToListOfStringsConverter" />
        </ResourceDictionary>
    </Window.Resources>
    <Window.InputBindings>
        <KeyBinding Key="F5" Command="{Binding RefreshCommand}" />
        <KeyBinding Key="D" Modifiers="Control" Command="{Binding FindDuplicatesCommand}" />
        <KeyBinding Key="I" Modifiers="Control" Command="{Binding ImportXmlCommand}"  />
        <KeyBinding Key="O" Modifiers="Control" Command="{Binding OpenXmlFolderCommand}"  />
        <KeyBinding Key="F1" Command="{Binding AboutCommand}" />
        <KeyBinding Key="F3" Command="{Binding FocusFilterCommand}" />
        <KeyBinding Key="F" Modifiers="Control" Command="{Binding FocusFilterCommand}" />
        <KeyBinding Key="Escape" Command="{Binding ClearFilterCommand}" />
    </Window.InputBindings>
    
    <Grid x:Name="LayoutRoot" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Menu Grid.Row="0" Grid.ColumnSpan="3">
            <MenuItem Header="_File">
                <MenuItem Header="_Import XML" Command="{Binding ImportXmlCommand}" InputGestureText="Ctrl+I" >
                    <MenuItem.Icon>
                        <Image Source="Resources/Images/import.ico" Height="16" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Refresh" Command="{Binding RefreshCommand}" InputGestureText="F5" >
                    <MenuItem.Icon>
                        <Image Source="Resources/Images/refresh.ico" Height="16" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="_Open XML folder" Command="{Binding OpenXmlFolderCommand}" InputGestureText="Ctrl+O" />
                <Separator />
                <MenuItem Header="_Exit" Command="{Binding ExitCommand}" />
            </MenuItem>
            <MenuItem Header="_Recent files" ItemsSource="{Binding RecentFiles, Converter={StaticResource RecentFilesToListOfStringsConverter}, Mode=OneWay}" >
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Command" Value="{Binding DataContext.ImportRecentItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}" />
                        <Setter Property="CommandParameter" Value="{Binding}" />
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
        </Menu>
        <ToolBar Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"  x:Name="Toolbar" HorizontalAlignment="Left">
            <Button x:Name="ButtonImpmortXml" Command="{Binding ImportXmlCommand}" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="Resources/Images/import.ico" Height="24" />
                    <TextBlock Text=" " />
                    <TextBlock Text="Import XML" VerticalAlignment="Center" />
                </StackPanel>
                <Button.ToolTip>
                    <StackPanel>
                        <TextBlock Text="Ctrl+I" FontWeight="Bold" />
                        <TextBlock Text="Import repository XML" />
                    </StackPanel>
                </Button.ToolTip>
            </Button>
            <Button x:Name="ButtonRefresh" Command="{Binding RefreshCommand}" ToolTipService.ShowOnDisabled="True" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="Resources/Images/refresh.ico" Height="22" />
                    <TextBlock Text=" " />
                    <TextBlock Text="Refresh" VerticalAlignment="Center" />
                </StackPanel>
                <Button.ToolTip>
                    <StackPanel>
                        <TextBlock Text="F5" FontWeight="Bold" />
                        <TextBlock Text="Refresh (re-read XML and update the UI)" />
                    </StackPanel>
                </Button.ToolTip>
            </Button>
            <Separator />
            <CheckBox x:Name="ButtonFindDuplicates" Command="{Binding FindDuplicatesCommand}" ToolTipService.ShowOnDisabled="True" IsChecked="{Binding IsFindDuplicatesChecked}" >
                <StackPanel Orientation="Horizontal">
                    <Image Source="Resources/Images/duplicate.ico" Height="24" />
                    <TextBlock Text=" " />
                    <TextBlock Text="Find Duplicates" VerticalAlignment="Center" />
                </StackPanel>
                <CheckBox.ToolTip>
                    <StackPanel>
                        <TextBlock Text="Ctrl+D" FontWeight="Bold" />
                        <TextBlock Text="Find duplicated controls - which have the same properties, but different Ids" />
                    </StackPanel>
                </CheckBox.ToolTip>
            </CheckBox>
            <StackPanel x:Name="FilterControlsPanel" Orientation="Horizontal" Height="25" ToolTipService.ShowOnDisabled="True">
                <xctk:WatermarkTextBox Watermark="Filter controls" Width="120" x:Name="TextBoxFilter" Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}" 
                                       IsEnabled="{Binding ElementName=ButtonRefresh, Path=IsEnabled}"
                                       VerticalContentAlignment="Center" SelectAllOnGotFocus="True" >
                    <xctk:WatermarkTextBox.ToolTip>
                        <StackPanel>
                            <TextBlock Text="F3, Ctrl+F" FontWeight="Bold" />
                            <TextBlock Text="Go to filter" />
                        </StackPanel>
                    </xctk:WatermarkTextBox.ToolTip>
                </xctk:WatermarkTextBox>
                <Button x:Name="ButtonClearFilter" Command="{Binding ClearFilterCommand}" ToolTipService.ShowOnDisabled="True">
                    <Image Source="Resources/Images/clear.ico" Height="16" />
                    <Button.ToolTip>
                        <StackPanel>
                            <TextBlock Text="Escape" FontWeight="Bold" />
                            <TextBlock Text="Clear filter" />
                        </StackPanel>
                    </Button.ToolTip>
                </Button>
            </StackPanel>
            <Button x:Name="ButtonCopyGridSelectedRowToClipboard" Command="Copy" ToolTipService.ShowOnDisabled="True">
                <Image Source="Resources/Images/clipboard.ico" />
                <Button.ToolTip>
                    <StackPanel>
                        <TextBlock Text="Ctrl+C" FontWeight="Bold" />
                        <TextBlock Text="Copy selected datagrid row to clipboard" />
                    </StackPanel>
                </Button.ToolTip>
            </Button>
            <Separator />
            <Button x:Name="ButtonAbout" Command="{Binding AboutCommand}" ToolTipService.ShowOnDisabled="True" >
                <Image Source="Resources/Images/about.png" Height="24" />
                <Button.ToolTip>
                    <StackPanel>
                        <TextBlock Text="F1" FontWeight="Bold" />
                        <TextBlock Text="About White Repository Explorer" />                                    
                    </StackPanel>
                </Button.ToolTip>
            </Button>
        </ToolBar>
        
        <DataGrid x:Name="DataGridControls" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding Controls}" 
                AutoGenerateColumns="False" CanUserAddRows="False" CanUserResizeRows="False"
                CanUserDeleteRows="False" CanUserSortColumns="True" GridLinesVisibility="None"
                SelectedItem="{Binding SelectedControl, Mode=TwoWay}" 
                SelectedIndex="{Binding SelectedIndex}" ClipboardCopyMode="ExcludeHeader"
                Background="White" Margin="3" IsReadOnly="False" HeadersVisibility="Column" 
                SelectionUnit="FullRow" SelectionMode="Extended" FontSize="12" >
            
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Type" SortMemberPath="ControlType" 
                                        ClipboardContentBinding="{Binding ControlType}"  >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image>
                                    <Image.Style>
                                        <Style TargetType="{x:Type Image}" >
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Button" >
                                                    <Setter Property="Source" Value="Resources/Images/button.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Window" >
                                                    <Setter Property="Source" Value="Resources/Images/window.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="DataGrid" >
                                                    <Setter Property="Source" Value="Resources/Images/grid.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Tree" >
                                                    <Setter Property="Source" Value="Resources/Images/tree.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Text" >
                                                    <Setter Property="Source" Value="Resources/Images/label.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Edit" >
                                                    <Setter Property="Source" Value="Resources/Images/edit.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="CheckBox" >
                                                    <Setter Property="Source" Value="Resources/Images/checkbox.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="ComboBox" >
                                                    <Setter Property="Source" Value="Resources/Images/combobox.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="List" >
                                                    <Setter Property="Source" Value="Resources/Images/list.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Hyperlink" >
                                                    <Setter Property="Source" Value="Resources/Images/hyperlink.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Group" >
                                                    <Setter Property="Source" Value="Resources/Images/group.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Tab" >
                                                    <Setter Property="Source" Value="Resources/Images/tab.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="TreeItem" >
                                                    <Setter Property="Source" Value="Resources/Images/treeItem.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="Custom" >
                                                    <Setter Property="Source" Value="Resources/Images/custom.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="ToolBar" >
                                                    <Setter Property="Source" Value="Resources/Images/toolbar.png" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="MenuBar" >
                                                    <Setter Property="Source" Value="Resources/Images/menuBar.ico" />
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding ControlType}" Value="MenuItem" >
                                                    <Setter Property="Source" Value="Resources/Images/menuItem.ico" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                                <TextBlock Text=" " />
                                <TextBlock Text="{Binding Path=ControlType, Mode=OneWay}" />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Id" Binding="{Binding Id, Mode=OneWay}" CanUserSort="True" />
                <DataGridTextColumn Header="AutomationId" Binding="{Binding Properties.AutomationId, Mode=OneWay}" CanUserSort="True" />
                <DataGridTextColumn Header="Name" Binding="{Binding Properties.Name, Mode=OneWay}" CanUserSort="True" />
                <DataGridTextColumn Header="Image" Binding="{Binding ImagePath, Mode=OneWay}" CanUserSort="True" />
            </DataGrid.Columns>
        </DataGrid>

        <Border BorderThickness="1" BorderBrush="Silver" Grid.Row="3" Grid.ColumnSpan="4" Margin="2,0,2,0">
            <Grid x:Name="PathToDllGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding PathToXml}" Grid.Column="0" x:Name="TextPathToXml" />
                <Button Content="..." Grid.Column="1" Command="{Binding OpenXmlFolderCommand}" Width="25" ToolTipService.ShowOnDisabled="True" >
                    <Button.ToolTip>
                        <StackPanel>
                            <TextBlock Text="Ctrl+O" FontWeight="Bold" />
                            <TextBlock Text="Open XML containing folder and select the XML" />
                        </StackPanel>
                    </Button.ToolTip>
                </Button>
            </Grid>
        </Border>

        <StatusBar Grid.Row="4" Grid.ColumnSpan="4" x:Name="StatusBar">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem Grid.Column="0">
                <TextBlock Text="Count" />
            </StatusBarItem>
            <StatusBarItem Grid.Column="1">
                <TextBox Text="{Binding Controls.Count, Mode=OneWay}" IsReadOnly="True" Width="50" Height="20" Background="LightGray" />
            </StatusBarItem>
            <StatusBarItem Grid.Column="2" HorizontalAlignment="Right">
                <Button Command="{Binding ExitCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="Resources/Images/clear.ico" />
                        <TextBlock Text=" " />
                        <TextBlock Text="Exit" />
                    </StackPanel>
                </Button>
            </StatusBarItem>
        </StatusBar>

        <GridSplitter Grid.Row="2" Grid.Column="1" Width="3" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"   />
        
        <Grid x:Name="SidebarGrid" Grid.Row="2" Grid.Column="2" Margin="3" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" MinHeight="100" />
                <RowDefinition Height="Auto" MinHeight="100" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="70" />
            </Grid.RowDefinitions>
            <xctk:PropertyGrid x:Name="PropertyGridControlItself" Grid.Row="0" ShowSearchBox="False" ShowSortOptions="False" SelectedObject="{Binding SelectedControl}" 
                    ShowSummary="True" SelectedObjectName="{Binding SelectedControl.Id, Mode=OneWay}" IsReadOnly="False" IsCategorized="False"
                               IsManipulationEnabled="True" ShowAdvancedOptions="False" Margin="0,0,0,2" >
            </xctk:PropertyGrid>
            <xctk:PropertyGrid x:Name="PropertyGridControlAutomationProperties" Grid.Row="1" ShowSearchBox="False" ShowSortOptions="False" ShowSummary="False" IsCategorized="False"
                               SelectedObject="{Binding SelectedControl.Properties}" SelectedObjectName="Automation properties" Margin="0,0,0,2">
                <xctk:PropertyGrid.AdvancedOptionsMenu>
                    <ContextMenu DataContext="{Binding}" HasDropShadow="True" IsManipulationEnabled="True" UseLayoutRounding="False" />
                </xctk:PropertyGrid.AdvancedOptionsMenu>
                <xctk:PropertyGrid.ContextMenu>
                    <ContextMenu />
                </xctk:PropertyGrid.ContextMenu>
            </xctk:PropertyGrid>
            <GridSplitter Grid.Row="2" Height="3" ResizeDirection="Rows" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
            <Border Grid.Row="3" Margin="0" BorderThickness="1" BorderBrush="Silver">
                <Image x:Name="ImageScreenshot" Margin="5">
                    <Image.Source>
                        <MultiBinding Converter="{StaticResource ImageNameToPathConverter}">
                            <Binding Path="SelectedControl.ImagePath" />
                            <Binding Path="PathToXml" />
                        </MultiBinding>
                    </Image.Source>
                </Image>
            </Border>
            <ScrollViewer Grid.Row="4" VerticalScrollBarVisibility="Auto">
                <Grid Grid.Row="4">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Label Content="Description" FontWeight="Bold" Grid.Row="0" />
                    <Border BorderThickness="0,0,0,1" BorderBrush="Silver" Grid.Row="1">
                        <TextBlock Text="{Binding SelectedControl.Description}" TextWrapping="Wrap"   />
                    </Border>
                </Grid>
            </ScrollViewer>
        </Grid>

    </Grid>
</Window>
